import os
import GameLogic
import time

# init each object needed
cont = GameLogic.getCurrentController()
me   = cont.getOwner()
tick = cont.getSensor('Tick')
send  = cont.getActuator('sendmess')

# when we receive a command we look for : 
# - an action verb
# - keywords

# TODO : implements good test suite like in diveintopython
# List of all the available actions of the model
actions = (('verb','keyword1','keyword2','type','action name','sound'),
           ('scenario','test','','scenario','test.txt','alizee-jenaimarre.mp3'),
           ('marcher','','','action','walkcycle',''),
           ('walk','','','action','walkcycle',''),
           ('attendre','','','action','waitup',''),
           ('wait','','','action','waitup',''),
           ('attendre','haut','','action','waitup',''),
           ('wait','up','','action','waitup',''),
           ('attendre','bas','','action','waitdown',''),
           ('sexy','','','action','waitdown',''),
           ('wait','down','','action','waitdown',''),
           ('fessee','','','action','spank',''),
           ('spank','','','action','spank',''),
           ('saluer','','','action','wave',''),
           ('wave','','','action','wave',''),
           ('bonjour','','','action','wave',''),
           ('hello','','','action','wave',''),
           ('bye','','','action','wave',''),
           ('sing','alizee','marre','action','marre','alizee-jenaimarre.mp3'),
           ('chanter','alizee','marre','action','marre','alizee-jenaimarre.mp3'),
           ('dance','alizee','marre','action','marre','alizee-jenaimarre.mp3'),
           ('dancer','alizee','marre','action','marre','alizee-jenaimarre.mp3'),
           ('sing','alizee','lolita','action','lolita','alizee-moilolita.mp3'),
           ('chanter','alizee','lolita','action','lolita','alizee-moilolita.mp3'),
           ('dance','alizee','lolita','action','lolita','alizee-moilolita.mp3'),
           ('dancer','alizee','lolita','action','lolita','alizee-moilolita.mp3'),
           ('','','','action','',''))

# function that plays a scenario of actions
# send multiple messages with delay to play the action and a sound if exist
def playscenario(scenario):
	"""open the scenario file if exists"""
	if os.path.exists(scenario):
		command = "echo '" +  scenario + "' > scenario.txt"
		print command
		os.system(command)
	
# function that find an action in the tuples aboves
# send a message to play the action and a sound if exist
def findaction(text):
	"""find verb and keyword to return the action"""
	textlist = text.split()
	type=''
	action=''
	print "we are in find action"   
	# find verb
	for i in range(len(textlist)):
		# a keyword is located in the 3 following words
		print textlist[i]
		# word,textlist[i+1],textlist[i+2],textlist[i+3])
		for column in actions:
			# if we find a verb and keywords get the best suitable action or scenario
			if textlist[i] == column[0]:
				type=column[3]
				action=column[4]
				sound=column[5]
				if textlist[i+1] == column[1]:
					type=column[3]
					action=column[4]
					sound=column[5]
					if textlist[i+2] == column[2]:
						type=column[3]
						action=column[4]
						sound=column[5]				
						print 'action finale break : ' + action
						print 'type action : ' + type
						if type == 'action':
							send.setBody(action)
							GameLogic.addActiveActuator(send,1)
						if type == 'scenario':
							playscenario(action)						
						# Submit a sound to play with mplayer
						command = "echo '" +  sound + "' > sound.txt"
						print command
						os.system(command)
						break # if exact matches so play and break
				# not exact matches so play what we found				
				print 'action finale : ' + action
				if type == 'action':
					send.setBody(action)
					GameLogic.addActiveActuator(send,1)
				if type == 'scenario':
					playscenario(action)						
				# Submit a sound to play with mplayer
				command = "echo '" +  sound + "' > sound.txt"
				print command
				os.system(command)

# check if lolita programd irc answered
if os.path.exists('receive.txt'):
	f = open("receive.txt", "r")
	text = f.read()
	me.message = text
	f.close()
	os.remove('receive.txt')
	# Search for the action to play
	findaction(me.message)

# each tick turn around the text for the user
text = ""
if tick.isPositive():
	me.pos = me.pos + 1
	if me.pos == len(me.message):
		me.pos = 0
	for i in xrange(60):
		text = text + me.message[(me.pos+i)%len(me.message)]
		me.Text = text
